package com.ycy.util;

import java.util.Comparator;
import java.util.Map;

/**
 * @project krimTop
 * 
 * @author <a href="mailto:yuanchongyu1@163.com">admin</a>
 * 
 * @createDate 2011-3-1 上午10:53:02
 * 
 * @param <T>
 */
public class ValueSortComparator<T extends Map.Entry<? extends Object, Integer>> implements Comparator<T>{

	private boolean asc = true;

	public ValueSortComparator(){

	}

	public ValueSortComparator(boolean asc){
		this.asc = asc;
	}

	@Override
	public int compare(T o1, T o2){
		if (o1 == null || o1.getValue() == null) {
			return -1;
		} else if (o2 == null || o2.getValue() == null) {
			return 1;
		}
		if (asc) {
			return o1.getValue().compareTo(o2.getValue()) == -1 ? -1 : 1;
		} else {
			return o2.getValue().compareTo(o1.getValue()) == -1 ? -1 : 1;
		}
	}
}
